package cn.bugstack.domain.order.service;

import cn.bugstack.domain.order.adapter.port.IProductPort;
import cn.bugstack.domain.order.adapter.repository.IOrderRepository;
import cn.bugstack.domain.order.model.aggregate.CreateOrderAggregate;
import cn.bugstack.domain.order.model.entity.OrderEntity;
import cn.bugstack.domain.order.model.entity.PayOrderEntity;
import cn.bugstack.domain.order.model.entity.ProductEntity;
import cn.bugstack.domain.order.model.entity.ShopCartEntity;
import cn.bugstack.domain.order.model.valobj.OrderStatusVO;
import com.alipay.api.AlipayApiException;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;

/**
 * @Author: 小新
 * @Date: 2025/3/22 13:33
 * @Description:
 * @Version: v1.0
 */


@Slf4j
public abstract class AbstractOrderService implements IOrderService{

    protected final IOrderRepository repository;

    protected final IProductPort port;

    public AbstractOrderService(IOrderRepository repository, IProductPort port) {
        this.repository = repository;
        this.port = port;
    }


    @Override
    public PayOrderEntity createOrder(ShopCartEntity shopCartEntity) throws Exception {
        // 1.当前用户是否存在掉单或未支付订单

        OrderEntity unpaidOrderEntity=repository.queryUnpaidOrder(shopCartEntity);

        //2.1 如果订单存在，但是未支付，则返回未支付订单
        if (null != unpaidOrderEntity && OrderStatusVO.PAY_WAIT.equals(unpaidOrderEntity.getOrderStatusVO())) {
            log.info("当前订单未支付，userId：{}, orderId: {}, productId: {}", shopCartEntity.getUserId(), unpaidOrderEntity.getOrderId(), shopCartEntity.getProductId());
            return PayOrderEntity.builder()
                    .orderId(unpaidOrderEntity.getOrderId())
                    .payUrl(unpaidOrderEntity.getPayUrl())
                    .build();
        }

        //2.2 如果订单存在
        if (null != unpaidOrderEntity && OrderStatusVO.CREATE.equals(unpaidOrderEntity.getOrderStatusVO())) {
            log.info("当前订单未支付,创建支付单开始，userId：{}, orderId: {}, productId: {}", shopCartEntity.getUserId(), unpaidOrderEntity.getOrderId(), shopCartEntity.getProductId());
            PayOrderEntity payOrderEntity = doPrePayOrder(shopCartEntity.getUserId(),unpaidOrderEntity.getOrderId(),unpaidOrderEntity.getProductId(),unpaidOrderEntity.getProductName(),unpaidOrderEntity.getTotalAmount());
            return PayOrderEntity.builder()
                    .orderId(payOrderEntity.getOrderId())
                    .payUrl(payOrderEntity.getPayUrl())
                    .build();
        }

        // 3.查询商品，创建订单
        ProductEntity productEntity=port.queryProductByProductId(shopCartEntity.getProductId());
        OrderEntity orderEntity = CreateOrderAggregate.buildOrderEntity(productEntity.getProductId(), productEntity.getProductName());

        CreateOrderAggregate createOrderAggregate = CreateOrderAggregate.builder()
                .userId(shopCartEntity.getUserId())
                .orderEntity(orderEntity)
                .productEntity(productEntity)
                .build();

        this.toSaveOrder(createOrderAggregate);
        PayOrderEntity payOrderEntity = doPrePayOrder(shopCartEntity.getUserId(),orderEntity.getOrderId(),productEntity.getProductId(),productEntity.getProductName(),productEntity.getPrice());
        return PayOrderEntity.builder()
                .orderId(orderEntity.getOrderId())
                .payUrl(payOrderEntity.getPayUrl())
                .build();
    }

    protected abstract PayOrderEntity doPrePayOrder(String userId, String orderId, String productId, String productName, BigDecimal price) throws AlipayApiException;

    protected abstract void toSaveOrder(CreateOrderAggregate createOrderAggregate);


}
